Skip to content

Preserve completed responses after compressed stream errors - #1723

Open
VIOLETee0 wants to merge 2 commits into
CodebuffAI:mainfrom
VIOLETee0:fix/completed-compressed-stream-1656
Open

VIOLETee0 wants to merge 2 commits into
CodebuffAI:mainfrom
VIOLETee0:fix/completed-compressed-stream-1656

Conversation

@VIOLETee0

Copy link
Copy Markdown

Refs #1656.

Bun can report ZlibError after a chat-completions response has already delivered its answer, a recognized finish reason, and token usage. The provider's pipeThrough aborts the converter before flush, discarding that completion metadata. A real loopback reproduction on main then makes promptAiSdkStream emit stream-interrupted and request continuation of an already completed answer.

Allow the converter to close normally on a gzip/br decompression error only after a recognized finish status and finite usage have been parsed. Incomplete responses and other failures still abort the converter. Downstream cancellation continues to cancel the upstream body.

One regression serves a complete SSE answer with an unfinished gzip trailer and checks that text, finish status, and usage survive. Its partial-response control still rejects for the existing bounded recovery. The same final test fails on current main (05e1006dc) and passes with the patch.

Rechecked on Windows, Bun 1.3.14, using a temporary merge of current main (05e1006dc) and this branch:

  • 116 related provider, SDK recovery, and runtime interruption/cancellation tests passed.
  • llm-providers and SDK TypeScript checks passed.
  • SDK build, Windows Freebuff build, version, and official binary smoke passed.
  • A separate loopback SDK probe emits no recovery after completed responses, and still recovers a partial response. A downstream-cancel probe confirms upstream cancellation.
  • ESLint: 0 errors, 4 resolver-configuration warnings; git diff --check passed.
  • Both files have existing whole-file Prettier discrepancies on main; existing lines were retained to avoid unrelated reformatting. New portions were formatted.

Scope: this prevents an unnecessary continuation. Bun may still print its own decompression diagnostic to stderr, so this does not claim to resolve every symptom in #1656. No authenticated provider/TUI session or Linux/macOS build was run.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good diagnosis: swapping response.pipeThrough(transform) for a manual source.pipeTo(transform.writable, { preventAbort: true }) is the right lever, since pipeThrough would abort the writable (and thus skip flush()) on any upstream read error, while a manual pipe lets you choose writer.close() vs writer.abort() based on whether finish/usage were already observed. The completed() gate (finish reason recognized + at least one finite usage field) is a reasonable heuristic, and restricting the close-instead-of-abort path to ZlibError/BrotliDecompressionError specifically keeps other transport failures aborting as before. Good that the 'partial' control case in the new test asserts the existing recovery path is untouched, and that cancellation semantics are preserved via default preventCancel: false.

Two things worth a closer look before porting:

  1. openai-compatible-chat-language-model.test.ts relies on a real HTTP server plus a setTimeout(20) before res.end() to force the trailer-cut timing. That's inherently more flake-prone than the file's existing synthetic SSE fixtures — worth confirming it's stable across a few CI runs, or at least isolating it with a generous timeout.
  2. completed() reads usage.promptTokens/usage.completionTokens/usage.totalTokens, while the final emitted usage uses inputTokens/outputTokens naming (per the test assertions). This is presumably mapping an internal OpenAI-shaped accumulator that isn't visible in this diff — please double check that these field names actually match the in-scope variable, since a silent mismatch would make completed() always false and this patch a no-op.

No forbidden paths touched, diff is small and self-contained, and the repro plus regression test make the intent to port easy to verify. Worth a maintainer's time once the above is confirmed.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Nov 25, 2026

Copy link
Copy Markdown
Author

I checked both points against the PR head and reproduced a timing failure in the original HTTP fixture on the first local repeat.

The updated fixture removes the 20 ms wait. For the completed response, the server cuts the gzip body only after the consumer observes the raw usage frame; the provider has already recorded usage in that synchronous transform, after the preceding finish frame. For the partial response, it cuts after the text delta is observed. The gzip trailer remains unfinished in both cases.

The revised regression still fails against unmodified upstream production code. With this patch, it passed 20 separate-process runs on Windows/Bun 1.3.14; all 9 tests in the provider file and the provider TypeScript check pass. This is local repetition evidence, not cross-platform CI evidence: Public CI is still awaiting approval.

The accumulator field names also match the gate: the accumulator declares promptTokens/completionTokens, the transform assigns them from prompt_tokens/completion_tokens, and flush maps them to inputTokens/outputTokens. The completion check reads that same accumulator. No production-code change was needed for this follow-up.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants